package com.example.lib.c1;

/**
 * slepp捕获InterruptedException异常的时候 isInterrupted中断标志位被清空了
 */
public class Main7Interrupt {

    private static class Thread1 extends Thread {
        @Override
        public void run() {
            super.run();
            String name = getName();
            while (!isInterrupted()) {
                System.out.println(name + "---- is running " + isInterrupted());
                try {
                    sleep(1000);//中断标记被清除
                } catch (InterruptedException e) {
                    //这里已经清除中断标记
                    interrupt();//重新设置一次中断标记
                    e.printStackTrace();
                }
            }
            System.out.println(name + "---- is end " + isInterrupted());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread1 thread1 = new Thread1();
        thread1.start();
        Thread.sleep(3000);
        thread1.interrupt();//注意这里是在main线程调用的，发起中断信号
    }

}
